package com.gd.iot.energy.service.systemadmin.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gd.iot.energy.code.ResponseCode;
import com.gd.iot.energy.consts.SystemConsts;
import com.gd.iot.energy.dao.systemadmin.EquipmentmodelPOMapper;
import com.gd.iot.energy.exception.MyException;
import com.gd.iot.energy.po.systemadmin.EquipmentmodelPO;
import com.gd.iot.energy.pojo.common.vo.ColumnVO;
import com.gd.iot.energy.pojo.common.vo.PageVO;
import com.gd.iot.energy.pojo.systemadmin.dto.EquipmentmodelSaveDTO;
import com.gd.iot.energy.pojo.systemadmin.vo.EnumVO;
import com.gd.iot.energy.pojo.systemadmin.vo.EquipmentmodelPageVO;
import com.gd.iot.energy.pojo.systemadmin.vo.EquipmentmodelVO;
import com.gd.iot.energy.pojo.systemadmin.vo.SupplierVO;
import com.gd.iot.energy.service.systemadmin.EquipmentmodelService;
import com.gd.iot.energy.service.systemadmin.SupplierService;
import com.gd.iot.energy.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 区域
 *
 * @author kris
 * @date Created at 8:33 下午 2020/8/5
 */
@Service
public class EquipmentmodelServiceImpl extends ServiceImpl<EquipmentmodelPOMapper, EquipmentmodelPO> implements EquipmentmodelService {

    @Resource
    private EquipmentmodelPOMapper equipmentmodelPOMapper;

    @Autowired
    private SupplierService supplierService;

    /**
     * 表头文件
     */
    private static final String EQUIPMENTMODEL_LIST_COLUMN_FILE_NAME = "systemadmin/equipmentmodel_list.column";

    @Override
    public List<EquipmentmodelVO> getEquipmentmodelVOs(String name) {
        QueryWrapper<EquipmentmodelPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", SystemConsts.BooleanEnum.FALSE.value);
        if (StrUtil.isNotBlank(name)) {
            queryWrapper.like("name", name);
        }
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        List<EquipmentmodelPO> equipmentmodelPOS = equipmentmodelPOMapper.selectList(queryWrapper);
        List<EquipmentmodelVO> equipmentmodelVOList = new ArrayList<>(equipmentmodelPOS.size());
        CustomBeanUtils.convertPojo(equipmentmodelPOS,equipmentmodelVOList,EquipmentmodelVO.class);
        return equipmentmodelVOList;
    }

    @Override
    public PageVO<EquipmentmodelPageVO> getEquipmentmodelPageVOs(String name, Integer equipmentType, Integer pageSize,
                                                                 Integer pageNo, String orderColumn, String orderType) {
        IPage<EquipmentmodelPO> page = new Page<>(pageNo, pageSize);
        QueryWrapper<EquipmentmodelPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", SystemConsts.BooleanEnum.FALSE.value);
        if (StrUtil.isNotBlank(name)) {
            queryWrapper.like("name", name.trim());
        }
        if (SystemConsts.EquipmentType.getEnumByValue(equipmentType) != null) {
            queryWrapper.eq("equipment_type", equipmentType);
        }
        if (StrUtil.isNotBlank(orderColumn) && StrUtil.isNotBlank(orderType)) {
            orderColumn = CustomStrUtil.humpToLine(orderColumn);
            queryWrapper.orderBy(true, orderType.equals(SystemConsts.OrderType.ASC.value), orderColumn);
        }
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        IPage<EquipmentmodelPO> equipmentmodelPOIPage = equipmentmodelPOMapper.selectPage(page, queryWrapper);
        PageVO<EquipmentmodelPageVO> pageVO = new PageVO<>();
        pageVO.setPageSize(pageSize);
        pageVO.setPageNum(pageNo);
        List<EquipmentmodelPageVO> roleVOList = convertToEquipmentmodelPageVOList(equipmentmodelPOIPage.getRecords(), pageSize, pageNo);
        pageVO.setData(roleVOList);
        pageVO.setTotal(equipmentmodelPOIPage.getTotal());
        pageVO.setPages(equipmentmodelPOIPage.getPages());

        //表头
        List<ColumnVO> columns = CustomColumnUtil.getColumn(EQUIPMENTMODEL_LIST_COLUMN_FILE_NAME);
        pageVO.setColumns(columns);
        return pageVO;
    }

    /**
     * 对象转换
     *
     * @param records
     * @return
     */
    private List<EquipmentmodelPageVO> convertToEquipmentmodelPageVOList(List<EquipmentmodelPO> records, Integer pageSize, Integer pageNo) {
        List<SupplierVO> supplierVOs = supplierService.getSupplierVOs(null);
        Map<Long, String> supplierMap = supplierVOs.stream().collect(
                Collectors.toMap(SupplierVO::getId, SupplierVO::getSupplierName, (key1, key2) -> key2));
        List<EquipmentmodelPageVO> res = new ArrayList<>(records.size());
        EquipmentmodelPageVO equipmentmodelPageVO;
        int i = pageSize * (pageNo - 1) + 1;
        for (EquipmentmodelPO record : records) {
            equipmentmodelPageVO = new EquipmentmodelPageVO();
            equipmentmodelPageVO.setSerialNum(i);
            equipmentmodelPageVO.setId(record.getId());
            equipmentmodelPageVO.setName(record.getName());
            equipmentmodelPageVO.setSupplierName(supplierMap.get(record.getSupplier()));
            equipmentmodelPageVO.setEquipmentTypeDesc(SystemConsts.EquipmentInstructionType.getDescByValue(record.getEquipmentType()));
            equipmentmodelPageVO.setEquWeight(record.getEquWeight());
            equipmentmodelPageVO.setEquSize(record.getEquSize());
            equipmentmodelPageVO.setMemo(record.getMemo());
            res.add(equipmentmodelPageVO);
            i++;
        }
        return res;
    }

    @Override
    public Long add(EquipmentmodelSaveDTO equipmentmodelSaveDTO) {
        if (equipmentmodelSaveDTO.getEquSize() == null ||
                equipmentmodelSaveDTO.getEquWeight() == null ||
                StrUtil.isBlank(equipmentmodelSaveDTO.getSerialnumber())) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        EquipmentmodelPO equipmentmodelPO = new EquipmentmodelPO();
        equipmentmodelPO.setSerialnumber(equipmentmodelSaveDTO.getSerialnumber());
        equipmentmodelPO.setCreateDate(new Date());
        equipmentmodelPO.setModifyDate(new Date());
        equipmentmodelPO.setEnergyType(SystemConsts.EnergyTypeEnum.WATER.value);
        equipmentmodelPO.setIsDel(SystemConsts.BooleanEnum.FALSE.value);
        //todo 后期看下公司ID怎么取
        equipmentmodelPO.setCompanyId(0L);
        //todo 后期看下操作人ID怎么取
        equipmentmodelPO.setOperatorId(1L);

        equipmentmodelPO.setName(equipmentmodelSaveDTO.getName());
        equipmentmodelPO.setSupplier(equipmentmodelSaveDTO.getSupplier());
        equipmentmodelPO.setEquWeight(equipmentmodelSaveDTO.getEquWeight());
        equipmentmodelPO.setEquSize(equipmentmodelSaveDTO.getEquSize());
        equipmentmodelPO.setMemo(equipmentmodelSaveDTO.getMemo());
        equipmentmodelPO.setDeviceType(equipmentmodelSaveDTO.getDeviceType());
        equipmentmodelPO.setArchivesLen(equipmentmodelSaveDTO.getArchivesLen());
        equipmentmodelPO.setEquipmentType(equipmentmodelSaveDTO.getEquipmentType());

        equipmentmodelPO.setApiKey(equipmentmodelSaveDTO.getApiKey());
        equipmentmodelPO.setAppId(equipmentmodelSaveDTO.getAppId());
        equipmentmodelPO.setDownLinkurl(equipmentmodelSaveDTO.getDownLinkurl());
        equipmentmodelPO.setEquipmentnbtype(equipmentmodelSaveDTO.getEquipmentnbtype());
        equipmentmodelPO.setSecret(equipmentmodelSaveDTO.getSecret());
        equipmentmodelPO.setManufacturerId(equipmentmodelSaveDTO.getManufacturerId());
        equipmentmodelPO.setManufacturerName(equipmentmodelSaveDTO.getManufacturerName());
        equipmentmodelPO.setModel(equipmentmodelSaveDTO.getModel());
        equipmentmodelPO.setProtocolType(equipmentmodelSaveDTO.getProtocolType());
        equipmentmodelPO.setDomainName(equipmentmodelSaveDTO.getDomainName());
        equipmentmodelPO.setAccessToken(equipmentmodelSaveDTO.getAccessToken());
        equipmentmodelPO.setEquipmentloratype(equipmentmodelSaveDTO.getEquipmentloratype());

        equipmentmodelPOMapper.insert(equipmentmodelPO);
        return equipmentmodelPO.getId();
    }

    @Override
    public boolean update(EquipmentmodelSaveDTO equipmentmodelSaveDTO) {
        if (equipmentmodelSaveDTO.getEquSize() == null ||
                equipmentmodelSaveDTO.getEquWeight() == null ||
                StrUtil.isBlank(equipmentmodelSaveDTO.getSerialnumber())) {
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        EquipmentmodelPO equipmentmodelPO = new EquipmentmodelPO();
        equipmentmodelPO.setId(equipmentmodelSaveDTO.getId());
        //todo 后期看下公司ID怎么取
        equipmentmodelPO.setCompanyId(0L);
        //todo 后期看下操作人ID怎么取
        equipmentmodelPO.setOperatorId(1L);
        equipmentmodelPO.setModifyDate(new Date());

        equipmentmodelPO.setName(equipmentmodelSaveDTO.getName());
        equipmentmodelPO.setSupplier(equipmentmodelSaveDTO.getSupplier());
        equipmentmodelPO.setEquWeight(equipmentmodelSaveDTO.getEquWeight());
        equipmentmodelPO.setEquSize(equipmentmodelSaveDTO.getEquSize());
        equipmentmodelPO.setMemo(equipmentmodelSaveDTO.getMemo());
        equipmentmodelPO.setDeviceType(equipmentmodelSaveDTO.getDeviceType());
        equipmentmodelPO.setArchivesLen(equipmentmodelSaveDTO.getArchivesLen());
        equipmentmodelPO.setEquipmentType(equipmentmodelSaveDTO.getEquipmentType());

        equipmentmodelPO.setApiKey(equipmentmodelSaveDTO.getApiKey());
        equipmentmodelPO.setAppId(equipmentmodelSaveDTO.getAppId());
        equipmentmodelPO.setEquipmentnbtype(equipmentmodelSaveDTO.getEquipmentnbtype());
        equipmentmodelPO.setSecret(equipmentmodelSaveDTO.getSecret());
        equipmentmodelPO.setManufacturerId(equipmentmodelSaveDTO.getManufacturerId());
        equipmentmodelPO.setManufacturerName(equipmentmodelSaveDTO.getManufacturerName());
        equipmentmodelPO.setModel(equipmentmodelSaveDTO.getModel());
        equipmentmodelPO.setProtocolType(equipmentmodelSaveDTO.getProtocolType());
        equipmentmodelPO.setDomainName(equipmentmodelSaveDTO.getDomainName());
        equipmentmodelPO.setAccessToken(equipmentmodelSaveDTO.getAccessToken());
        equipmentmodelPO.setDownLinkurl(equipmentmodelSaveDTO.getDownLinkurl());
        equipmentmodelPO.setEquipmentloratype(equipmentmodelSaveDTO.getEquipmentloratype());

        equipmentmodelPOMapper.updateById(equipmentmodelPO);
        return true;
    }

    @Override
    public boolean delete(List<Integer> ids) {
        if(CollUtil.isEmpty(ids)){
            throw new MyException(ResponseCode.PARAMETER_REQUIRED);
        }

        //设备型号表逻辑删除
        QueryWrapper<EquipmentmodelPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        EquipmentmodelPO equipmentmodelPO = new EquipmentmodelPO();
        equipmentmodelPO.setIsDel(SystemConsts.BooleanEnum.TRUE.value);
        int num = equipmentmodelPOMapper.update(equipmentmodelPO,queryWrapper);

        return num > 0;
    }

    @Override
    public EquipmentmodelSaveDTO getEquipmentmodel(Integer id) {
        EquipmentmodelPO equipmentmodelPO = equipmentmodelPOMapper.selectById(id);
        if(equipmentmodelPO==null||SystemConsts.BooleanEnum.TRUE.value.equals(equipmentmodelPO.getIsDel())){
            throw new MyException(ResponseCode.RECORD_NOT_EXIST);
        }

        EquipmentmodelSaveDTO equipmentmodelSaveDTO = new EquipmentmodelSaveDTO();
        equipmentmodelSaveDTO.setId(equipmentmodelPO.getId());
        equipmentmodelSaveDTO.setSerialnumber(equipmentmodelPO.getSerialnumber());
        equipmentmodelSaveDTO.setName(equipmentmodelPO.getName());
        equipmentmodelSaveDTO.setSupplier(equipmentmodelPO.getSupplier());
        equipmentmodelSaveDTO.setEquWeight(equipmentmodelPO.getEquWeight());
        equipmentmodelSaveDTO.setEquSize(equipmentmodelPO.getEquSize());
        equipmentmodelSaveDTO.setEquipmentType(equipmentmodelPO.getEquipmentType());
        equipmentmodelSaveDTO.setEquipmentnbtype(equipmentmodelPO.getEquipmentnbtype());
        equipmentmodelSaveDTO.setEquipmentloratype(equipmentmodelPO.getEquipmentloratype());
        equipmentmodelSaveDTO.setDomainName(equipmentmodelPO.getDomainName());
        equipmentmodelSaveDTO.setApiKey(equipmentmodelPO.getApiKey());
        equipmentmodelSaveDTO.setDownLinkurl(equipmentmodelPO.getDownLinkurl());
        equipmentmodelSaveDTO.setAccessToken(equipmentmodelPO.getAccessToken());
        equipmentmodelSaveDTO.setAppId(equipmentmodelPO.getAppId());
        equipmentmodelSaveDTO.setSecret(equipmentmodelPO.getSecret());
        equipmentmodelSaveDTO.setManufacturerId(equipmentmodelPO.getManufacturerId());
        equipmentmodelSaveDTO.setManufacturerName(equipmentmodelPO.getManufacturerName());
        equipmentmodelSaveDTO.setDeviceType(equipmentmodelPO.getDeviceType());
        equipmentmodelSaveDTO.setModel(equipmentmodelPO.getModel());
        equipmentmodelSaveDTO.setProtocolType(equipmentmodelPO.getProtocolType());
        equipmentmodelSaveDTO.setArchivesLen(equipmentmodelPO.getArchivesLen());
        equipmentmodelSaveDTO.setMemo(equipmentmodelPO.getMemo());

        return equipmentmodelSaveDTO;
    }

    @Override
    public Map<String, List<EnumVO>> getEnums() {
        Map<String, List<EnumVO>> resultMap = new HashMap<>();

        List<EnumVO> equipment = new ArrayList<>();
        for (SystemConsts.EquipmentType value : SystemConsts.EquipmentType.values()) {
            EnumVO vo = new EnumVO();
            vo.setValue(value.value);
            vo.setValueName(value.valueName);
            vo.setDesc(value.desc);
            equipment.add(vo);
        }

        List<EnumVO> equipmentNB = new ArrayList<>();
        for (SystemConsts.EquipmentNBType value : SystemConsts.EquipmentNBType.values()) {
            EnumVO vo = new EnumVO();
            vo.setValue(value.value);
            vo.setValueName(value.valueName);
            vo.setDesc(value.desc);
            equipmentNB.add(vo);
        }

        List<EnumVO> equipmentLORA = new ArrayList<>();
        for (SystemConsts.EquipmentLORAType value : SystemConsts.EquipmentLORAType.values()) {
            EnumVO vo = new EnumVO();
            vo.setValue(value.value);
            vo.setValueName(value.valueName);
            vo.setDesc(value.desc);
            equipmentLORA.add(vo);
        }

        resultMap.put("equipment", equipment);
        resultMap.put("equipmentNB", equipmentNB);
        resultMap.put("equipmentLORA", equipmentLORA);
        return resultMap;
    }
}
